package net.wangds.auth.account.comp;

import net.wangds.auth.account.entity.GeneralAccountEntity;
import net.wangds.auth.module.User;
import net.wangds.auth.module.UserStatusEnum;
import net.wangds.auth.account.repo.rdbms.AccountJpaRepo;
import net.wangds.auth.account.repo.rdbms.AccountUserRefJpaRepo;
import net.wangds.data.DisableEnum;
import net.wangds.data.RemovedEnum;
import net.wangds.jpa.comp.AbstractRemovedComp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class GeneralAccountComp extends AbstractRemovedComp<GeneralAccountEntity> implements AccountComp<GeneralAccountEntity> {

    private final AccountJpaRepo mmRepoAccount;

    private final AccountUserRefJpaRepo mmRepoRefUser;

    @Autowired
    protected GeneralAccountComp(AccountJpaRepo repo, AccountUserRefJpaRepo refUsrRepo) {
        super(repo);
        this.mmRepoAccount = repo;
        this.mmRepoRefUser = refUsrRepo;
    }

    public List<User> listAccountUsers(GeneralAccountEntity acc){
       return mmRepoRefUser.findAllByAccountIdAndDisableAndRemoved(acc.getId(),
               DisableEnum.NORMAL,
                RemovedEnum.NORMAL)
               .stream().map(c->(User)c).collect(Collectors.toList());
    }

    public Optional<GeneralAccountEntity> findNormalUserByLoginName(String loginname){
        return mmRepoAccount.findByLoginnameAndStatusAndRemoved(loginname, UserStatusEnum.NORMAL, RemovedEnum.NORMAL);
    }


    @Override
    public Page<GeneralAccountEntity> findByKeywords(String keywords, Pageable paging) {
        //TODO 按照关键字查询
        return this.findAll(paging);
    }
}
